package org.algorithm.biubiu.wheel_1.数组.三月.第四周;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhx
 * @date 2022-03-21 星期一18:26
 *
 * url: https://leetcode-cn.com/problems/pascals-triangle/
 */
public class 杨辉三角I {

    public static void main(String[] args) {
        System.out.println(generate(5));
    }


    /**
     * 输入: numRows = 5
     * 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
     *
     * 输入: numRows = 1
     * 输出: [[1]]
     */
    public static List<List<Integer>> generate(int numRows) {
          List<List<Integer>> res = new ArrayList();
   
              if(numRows==1) {
                  res.add(Arrays.asList(1));
                  return res;
                  }
             if(numRows==2) {
                 res.add(Arrays.asList(1));
                 res.add(Arrays.asList(1,1));
                 return res;
                 }
       
        res.add(Arrays.asList(1));
        res.add(Arrays.asList(1,1));
        for(int i = 2;i<numRows;i++){
            List<Integer> once = new ArrayList();
            for(int j = 0;j<=i;j++){
               List<Integer>  upList=  res.get(i-1);
                once.add(sum(upList,j));
            }
            res.add(once);

        }
        return res;
    }

    public static int sum(List<Integer>list,int index){
        if(index-1<0) return 1;
        if(index>list.size()-1) return 1;
        return list.get(index)+ list.get(index-1);
    }
}
